package com.hxzy.tms.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.tms.action.vo.Page;
import com.hxzy.tms.action.vo.TeamVo;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;
import com.hxzy.tms.service.TeamService;
import com.hxzy.tms.session.SessionHolder;
import com.opensymphony.xwork2.Action;

public class TeamAction extends AbstractAction {

    private static final long serialVersionUID = 2581338981313142112L;
    private TeamVo team;
    private TeamService teamService;
    private TaskService taskService;
    private String email;
    private List<Team> teams;
    private String indexKey=null; 
    private Team teamList;
    private List<Task> taskList;
    private List<Project> projectList;
    private List<User> userList;
    private Date today;
    private String title;
    private Page page;
    private int p;
    
    //团队首页
    public String index(){
    	if(indexKey==null){
    		addActionError("参数丢失");
    		return ERROR;
    	}
    	Team t=teamService.read(new Long(indexKey)).data;
    	if(t==null){
    		addActionError("团队不存在");
    	}
    	teamList=t;
    	projectList=teamService.projectList(t.getId()).data;
    	userList=teamService.memberList(t.getId()).data;
    	taskList=taskService.read(t.getId()).data;
    	today=new Date();
    	return INPUT;
    }
    //创建团队
    public String post(){
    	title="创建团队";
        ServiceResult<Team> result = null;
        if (team.getId() != null) {
            result = teamService.update(team.getId(), team.getName(), team.getDesc(), team.getIntro(), 
                    team.getSubdomain(), team.getBeginDate(), team.getEndDate());
        }
        result=teamService.create(team.getName(), team.getDesc(), team.getIntro(), team.getSubdomain(), 
                team.getBeginDate(), team.getEndDate());
        if(result.status==1){
        	 this.team.setName(result.data.getName());
             this.team.setId(result.data.getId());
             return Action.SUCCESS;
        }else{
        	addActionError(result.message);
        	return ERROR;
        }
       
    }
    //所有团队
    public String list(){
        teams=teamService.list(1);
        return SUCCESS;
    }
    
    //我的团队
    public String myList(){
    	title="我的团队";
    	//this.teams=teamService.myLists(null).data;
    	   int count = teamService.count(SessionHolder.get().getId());
           page = new Page(count, 5, p);
           this.teams = teamService.myTeam(SessionHolder.get().getId(), page.getOffset(), page.getPageSize());
    	return SUCCESS;
    }
    //修改团队
    public String modify(){
    	title="修改团队";
        if (team != null || team.getId() != null) {
           Team team1 = teamService.read(team.getId()).data;
           this.team.setName(team1.getName());
           this.team.setDesc(team1.getDesc());
           this.team.setSubdomain(team1.getSubdomain());
           this.team.setStatus(team1.getStatus());
           this.team.setIntro(team1.getIntro());
           this.team.setBeginDate(new Date(team1.getGmtBegin() * 1000));
           this.team.setEndDate(new Date(team1.getGmtEnd() * 1000));
        }
        return INPUT;
    }
    //邀请成员
    public String invite(){
    	title="邀请成员";
    	if(isGET()){
    		if(team==null&&team.getId()==null){
    			addActionError("team id 参数错误");
    		}
    	}
    	if(isPOST()){
    	if(StringUtils.isBlank(email)){
    		addFieldError("email", "请输入想邀请用户的邮箱地址");
    	}
    	int result;
    	result=teamService.invite(SessionHolder.get().getId(), team.getId(), email);
    	switch (result) {
		case 1:
			addActionMessage("邀请成功");
			return Action.SUCCESS;
		case -1:
			addFieldError("email", "不能邀请自己");
			break;
		case -2:
			addFieldError("email", "用户已存在");
			break;
		case -3:
			addFieldError("email", "不是团队创建者，不能邀请用户");
			break;
		case -4:
			addFieldError("email", "邮箱对应用户不存在");
			break;
		}
    	}
    	return INPUT;
    }

    public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public List<Task> getTaskList() {
		return taskList;
	}
	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	public TeamVo getTeam() {
        return team;
    }

    public Page getPage() {
        return page;
    }
    public void setPage(Page page) {
        this.page = page;
    }
    public void setTeam(TeamVo team) {
        this.team = team;
    }

    public int getP() {
        return p;
    }
    public void setP(int p) {
        this.p = p;
    }
    public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Date getToday() {
		return today;
	}
	public void setToday(Date today) {
		this.today = today;
	}

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public List<Team> getTeams() {
		return teams;
	}
	public void setTeams(List<Team> teams) {
		this.teams = teams;
	}
	public TeamService getTeamService() {
        return teamService;
    }

    public String getIndexKey() {
		return indexKey;
	}
	public void setIndexKey(String indexKey) {
		this.indexKey = indexKey;
	}
	public void setTeamService(TeamService teamService) {
        this.teamService = teamService;
    }
	public Team getTeamList() {
		return teamList;
	}
	public void setTeamList(Team teamList) {
		this.teamList = teamList;
	}
	public List<Project> getProjectList() {
		return projectList;
	}
	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}


	
}

